package edu.zjucst.spb;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * mybatis3 代码生成器
 *
 * @author qingo
 * @date 2022/12/21 18:23
 */
public class CodeGenerator {
    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        tables.add("tb_self_activity");
        FastAutoGenerator.create("jdbc:mysql://81.68.143.166:3307/spb?serverTimezone=UTC", "root", "123456")
            .globalConfig(builder -> {
                builder.author("chb")               //作者
                    .outputDir(System.getProperty("user.dir") + "/spb-core-service/src/main/java")    //输出路径(写到java目录)
                    .enableSwagger()           //开启swagger
                    .commentDate("yyyy-MM-dd")
                    .fileOverride();            //开启覆盖之前生成的文件

            })
            .packageConfig(builder -> {
                builder.parent("edu.zjucst")
                    .moduleName("spb")
                    .entity("domain.entity")
                    .service("service")
                    .serviceImpl("service.impl")
                    .controller("controller")
                    .mapper("mapper")
                    .xml("mapper")
                    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper"));
            })
            .strategyConfig(builder -> {
                builder.addInclude(tables)
                    .addTablePrefix("tb_")
                    .serviceBuilder()
                    .formatServiceFileName("%sService")
                    .formatServiceImplFileName("%sServiceImpl")
                    .entityBuilder()
                    .enableLombok()
                    .logicDeleteColumnName("deleted")
                    .enableTableFieldAnnotation()
                    .controllerBuilder()
                    // 映射路径使用连字符格式，而不是驼峰
                    .enableHyphenStyle()
                    .formatFileName("%sController")
                    .enableRestStyle()
                    .mapperBuilder()
                    //生成通用的resultMap
                    .enableBaseResultMap()
                    .superClass(BaseMapper.class)
                    .formatMapperFileName("%sMapper")
                    .enableMapperAnnotation()
                    .formatXmlFileName("%sMapper");
            })
            .templateConfig(new Consumer<TemplateConfig.Builder>() {
                @Override
                public void accept(TemplateConfig.Builder builder) {
                    // 实体类使用我们自定义模板
                    builder.entity("templates/myentity.java");
                }
            })
            .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
            .execute();
        System.out.println("ok");
    }
}


